const { formatTime } = require("../../utils/util");

// pages/cartform/cartform.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartForm:[
      {id:"1",name:"工作日",type:1,fromtarget:'10:30',fromhome:'10:45'},
      {id:"2",name:"工作日",type:1,fromtarget:'11:00',fromhome:'11:15'},
      {id:"3",name:"工作日",type:1,fromtarget:'11:30',fromhome:'11:45'},
      {id:"4",name:"工作日",type:1,fromtarget:'12:00',fromhome:'14:15'},
      {id:"5",name:"工作日",type:1,fromtarget:'14:30',fromhome:'14:45'},
      {id:"6",name:"工作日",type:1,fromtarget:'15:00',fromhome:'15:15'},
      {id:"7",name:"工作日",type:1,fromtarget:'15:30',fromhome:'15:45'},
      {id:"8",name:"工作日",type:1,fromtarget:'16:00',fromhome:'16:15'},
      {id:"9",name:"节假日",type:2,fromtarget:'10:00',fromhome:'10:15'},
      {id:"10",name:"节假日",type:2,fromtarget:'10:30',fromhome:'10:45'},
      {id:"11",name:"节假日",type:2,fromtarget:'11:00',fromhome:'11:15'},
      {id:"12",name:"节假日",type:2,fromtarget:'11:30',fromhome:'11:45'},
      {id:"13",name:"节假日",type:2,fromtarget:'12:00',fromhome:'12:15'},
      {id:"14",name:"节假日",type:2,fromtarget:'12:30',fromhome:'12:45'},
      {id:"15",name:"节假日",type:2,fromtarget:'13:00',fromhome:'13:15'},
      {id:"16",name:"节假日",type:2,fromtarget:'14:00',fromhome:'14:15'},
      {id:"17",name:"节假日",type:2,fromtarget:'14:30',fromhome:'14:45'},
      {id:"18",name:"节假日",type:2,fromtarget:'15:00',fromhome:'15:15'},
      {id:"19",name:"节假日",type:2,fromtarget:'15:30',fromhome:'15:45'},
      {id:"20",name:"节假日",type:2,fromtarget:'16:00',fromhome:'16:15'},
      {id:"21",name:"节假日",type:2,fromtarget:'16:30',fromhome:'16:45'},
      {id:"22",name:"节假日",type:2,fromtarget:'17:00',fromhome:'17:15'},
      {id:"23",name:"节假日",type:2,fromtarget:'17:30',fromhome:'17:45'},
      {id:"24",name:"节假日",type:2,fromtarget:'18:00',fromhome:'18:15'},
      {id:"25",name:"节假日",type:2,fromtarget:'18:30',fromhome:'18:45'},
      {id:"26",name:"节假日",type:2,fromtarget:'19:00',fromhome:'19:15'},
      {id:"27",name:"节假日",type:2,fromtarget:'19:30',fromhome:'19:45'},
      {id:"28",name:"节假日",type:2,fromtarget:'20:00',fromhome:'20:15'}
    
    ],
    countdown:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.startTime();
  },
startTime(){
  var that = this;
  let endTime =new Date();
  if(endTime.getMinutes()>=30){
    endTime.setHours(endTime.getHours()+1);
    endTime.setMinutes(0);
  }else{
    endTime.setMinutes(30);
  }
  console.log( endTime.getHours(),endTime.getMinutes(),endTime.getSeconds());
  let now =new Date();
  var millisecond = endTime.getTime() -now.getTime();
  var interval = setInterval(function () {
    millisecond -= 1000;
    if (millisecond <= 0){
      clearInterval(interval);
      that.setData({
        countdown: {
          day: '00',
          hour: '00',
          minute: '00',
          second: '00'
        }
      });
      return;
    }
    that.transformRemainTime(millisecond);
  }, 1000);
},
 // 剩余时间(毫秒)处理转换时间
 transformRemainTime: function (millisecond) {
  var that = this;
  var countdownObj = that.data.countdown;
  // 秒数
  var seconds = Math.floor(millisecond / 1000);
  // 天数
  countdownObj.day = that.formatTime(Math.floor(seconds / 3600 / 24));
  // 小时
  countdownObj.hour = that.formatTime(Math.floor(seconds / 3600 % 24));
  // 分钟
  countdownObj.minute1 = that.formatTime(Math.floor(seconds / 60 % 60));
  if(countdownObj.minute1<=15){
    countdownObj.minute2 = that.formatTime(Math.floor(seconds / 60 % 60)+15);
  }else{
    var left =(parseInt(countdownObj.minute1)-15);
    console.log('time',left);
    countdownObj.minute2=that.formatTime(left);
  }
  
  // 秒
  countdownObj.second = that.formatTime(Math.floor(seconds % 60));
  that.setData({
    countdown: countdownObj
        });
  },
   //格式化时间为2位
   formatTime: function(time){
    if(time < 10)
      return '0' + time;
    return time;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})